package com.netease.nim.uikit.xpopup.popuview

import android.content.Context
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.Spanned
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.extension.toast
import www.wen.com.common.lib.extension.trimProxy
import www.wen.com.common.lib.helper.ResultUiCallBack
import java.util.regex.Pattern

/**
 * @author WEN
 * @Description:输入用户信息的弹框
 * @date 2021/3/3 14:53
 */
class InputContextPopupView(
        context: Context,
        val title: CharSequence,
        val content: CharSequence?,
        private val cancelContent: CharSequence,
        private val confirmContent: CharSequence,
        private val hintStr: CharSequence,
        maxLengthIn: Int = 8,
        private val finishOnCancel: Boolean = true,
        private val resultCallBack: ResultUiCallBack<String>? = null,
) : CenterPopupView(context), View.OnClickListener {

    private lateinit var cancelView: TextView

    private lateinit var enterView: TextView

    private lateinit var etContent: EditText

    private lateinit var tvTitle: TextView

    override fun getImplLayoutId(): Int = R.layout.popupview_input_layout

    override fun onClick(v: View) {
        when (v.id) {
            R.id.cancel -> {
                if (finishOnCancel) {
                    dismiss()
                }
            }
            R.id.confirm -> {
                resultCallBack?.invoke(v, etContent.trimProxy(), this)
            }
        }
    }

    override fun initPopupContent() {
        super.initPopupContent()

        cancelView = findViewById(R.id.cancel)
        enterView = findViewById(R.id.confirm)

        etContent = findViewById(R.id.tvContent)
        tvTitle = findViewById(R.id.tvTitle)

        etContent.filters = inputFilters
        etContent.hint = hintStr


        tvTitle.text = title
        cancelView.text = cancelContent
        enterView.text = confirmContent

        if(!content.isNullOrBlank()) {
            etContent.setText(content)
            etContent.setSelection(etContent.text.toString().length)
        }

        setOnClickListenerAgency(this, cancelView, enterView)
    }

    override fun getMaxWidth(): Int {
        return (XPopupUtils.getWindowWidth(context) * 0.8f).toInt()
    }


    private val inputFilters = arrayOf(
            object : InputFilter {
                var emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                        Pattern.UNICODE_CASE or Pattern.CASE_INSENSITIVE)

                override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
                    val emojiMatcher = emoji.matcher(source)
                    return if (emojiMatcher.find()) {
                        toast("不支持输入表情")
                        //                        Toast.makeText(FeedBackActivity.this, "不支持输入表情", Toast.LENGTH_SHORT).show();
                        //  MyToast.showText("不支持输入表情");
                        ""
                    } else null
                }
            },
            InputFilter { source, start, end, dest, dstart, dend ->
                for (i in start until end) {
                    val type = Character.getType(source[i])
                    if (type == Character.SURROGATE.toInt() || type == Character.OTHER_SYMBOL.toInt()) {
                        toast("不支持输入表情")
//                            Toast.makeText(FeedBackActivity.this, "不支持输入表情", Toast.LENGTH_SHORT).show();
                        return@InputFilter ""
                    }
                }
                null
            },
            /**这里限制输入的长度为200 */
            LengthFilter(maxLengthIn)
    )


}


